package com.example.pmanagesys.controller;
// 考勤记录
import com.example.pmanagesys.entity.Attendance;
import com.example.pmanagesys.entity.applyFor;
import com.example.pmanagesys.mapper.AttendanceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/attendance") //路径
public class AttendanceController {

    @Autowired
    private AttendanceMapper attendanceMapper;

    // 出勤记录页面
    @GetMapping("/file")
    public ModelAndView login() {
        return new ModelAndView("AttendanceRecord");
    }

    // 分页查询  localhost:8080/attendance/page?pageNum=1&pageSize=10&z_name=&startDate=&endDate=
    @GetMapping("/page")
    public Map<String, Object> findPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestParam String z_name,
                                        @RequestParam String startDate, @RequestParam String endDate)  {
        z_name = "%"+z_name+"%";
        // 某一页的数据
        pageNum = (pageNum - 1) * pageSize;
        List<Attendance> data = attendanceMapper.selectPage(pageNum, pageSize,z_name,startDate,endDate);
        // 总条数
        Integer total = attendanceMapper.selectTotal(z_name,startDate,endDate);
        // 封装一个map装数据
        Map<String, Object> res = new HashMap<>();
        res.put("AttendanceData", data);
        res.put("total",total);
        return res;
    }

    // admin 修改 （补签，请假，出差） http://localhost:8080/attendance
    @PostMapping
    public Integer update(@RequestBody Attendance attendance) {
        return attendanceMapper.update(attendance);
    }

    // admin 批量修改  http://localhost:8080/attendance/update/batch   发送json对象  {notes: '事由', ids: [1, 2, 3] }
    @PostMapping("/update/batch")
    public Integer updateBatch( @RequestBody Map<String, Object> requestMap) {
        String notes = (String) requestMap.get("notes");
        List<Integer> ids = (List<Integer>) requestMap.get("ids");
        return attendanceMapper.updateBatchByIds(notes,ids);
    }

    // user  提交申请 新增 （补签，请假，出差） http://localhost:8080/attendance/applyfor
    @PostMapping("/applyfor")
    public Integer add(@RequestBody applyFor applyFor) {
        attendanceMapper.userupdate(applyFor);// 提交申请后 修改补签状态为 '审批中‘
        return attendanceMapper.add(applyFor);
    }

    // 待办  获取对应申请列表  http://localhost:8080/attendance/applyfor/down?leaderName=
    @GetMapping("/applyfor/down")
    public Map<String, Object>  allFind(@RequestParam String leaderName) {
        Map<String, Object> res = new HashMap<>();
        List<applyFor> data = attendanceMapper.allFind(leaderName);
        Integer count = attendanceMapper.selectcount(leaderName);
        res.put("applyFor", data);
        res.put("count",count);
        return res;
    }

    // 待办 同意 修改   http://localhost:8080/attendance/applyfor/agree
    @PostMapping("/applyfor/agree")
    public Integer agreeUpdate( @RequestBody Map<String, Object> requestMap) {
        String overtime = (String) requestMap.get("overtime");
        String result = (String) requestMap.get("result");
        int id = (int) requestMap.get("id");
        return attendanceMapper.agreeUpdate(result,overtime,id);
    }

    // 待办 拒绝 修改   http://localhost:8080/attendance/applyfor/refuse?id=
    @PostMapping("/applyfor/refuse")
    public Integer refuseUpdate( @RequestParam Integer id) {
        return attendanceMapper.refuseUpdate(id);
    }

    // 消息  获取对应 消息通知  http://localhost:8080/attendance/applyfor/message?z_name=
    @GetMapping("/applyfor/message")
    public Map<String, Object>  messageFind(@RequestParam String z_name) {
        Map<String, Object> res = new HashMap<>();
        List<applyFor> data = attendanceMapper.messageFind(z_name);
        Integer count = attendanceMapper.messageCount(z_name);
        res.put("messageData", data);
        res.put("messageCount",count);
        return res;
    }

    // 消息 已读 修改   http://localhost:8080/attendance/applyfor/message/read?id=
    @PostMapping("/applyfor/message/read")
    public Integer read( @RequestParam Integer id) {
        return attendanceMapper.readUpdate(id);
    }


}

